<template>
  <div class="home-layout">
    <div class="home-content">
      <div class="header">
        <div class="logo" @click="$router.push('/course')">Logo</div>
        <div class="nav">
          <router-link to="/course">课程</router-link>
          <router-link to="/class">班级</router-link>
          <router-link to="/question">任务</router-link>
          <router-link to="/message">消息</router-link>
        </div>
        <div class="nav-right">
          <div v-if="!isLogin">
            <span @click="$router.push('/profile')">登录</span> | <span @click="$router.push('/register')">注册</span>
          </div>
          <div class="user-info" v-else>
            <img :src="$store.getters.avatar">
            <el-dropdown @command="handleCommand">
              <span class="el-dropdown-link">
                {{$store.getters.name}}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="/my-course">我的课程</el-dropdown-item>
                <el-dropdown-item command="/profile">个人中心</el-dropdown-item>
                <el-dropdown-item divided @click.native="logout">
                  <span>退出登录</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>

          </div>
        </div>
      </div>
      <div class="router-box">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'index',
  data() {
    return {
      isLogin: false
    }
  },
  created() {
    if (this.$store.getters.token) {
      this.isLogin = true
    }
  },
  methods:{
    handleCommand(r){
      this.$router.push(r);
    },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/';
        })
      }).catch(() => {});
    }
  }
}
</script>

<style scoped>
.home-layout {
  min-height: 100vh;
  background-color: #e9e9e9;
}

.home-content {
  background-color: #fff;
  width: 1200px;
  min-height: 100vh;
  margin: 0 auto;
  position: relative;
}

.header {
  height: 60px;
  display: flex;
  justify-content: space-around;
  box-shadow: 0 0 10px #ccc;
  width: 100%;
  line-height: 60px;
  position: sticky;
  top: 0;
  background-color: #fff;
  z-index: 999;
}

.logo {
  cursor: pointer;
}

.nav a {
  margin: 0 20px;
  padding: 5px;
  transition: all .3s;
}

.nav a:hover {
  color: #2f8f3e;
}

.router-link-active {
  border-bottom: 2px solid #2f8f3e;
}

.router-box {
  padding: 10px;
}

.nav-right span {
  cursor: pointer;
}

/*用户信息*/
.user-info {
  height: 100%;
  display: flex;
  align-items: center
}

.user-info img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}
</style>
